﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Emoney.UserTraining.BLL;
using Emoney.UserTraining.Comm;
using Emoney.UserTraining.Model;

namespace Emoney.UserTraining.Admin.Controllers
{
    public class TemporaryController : Controller
    {
        void PageHelper(BaseQuery query)
        {
            string strPage = StringTools.PageUrl(query.RecordCount, query.Index, query.PageSize);
            ViewData["strPage"] = strPage;
        }

        BaseQuery GetQuery(BaseQuery query)
        {
            int index = 1;
            if (!int.TryParse(Request["index"], out index))
            {
                index = 1;
            }
            if (index < 1)
            {
                index = 1;
            }
            query.Index = index;
            query.PageSize = query.PageSize == 0 ? 20 : query.PageSize;
            ViewData["PageIndex"] = (index - 1) * query.PageSize;
            string sortName = Request["SortName"];
            ViewData["sortName"] = sortName;
            query.SortName = sortName;
            string sortFlag = Request["SortFlag"];
            ViewData["SortFlag"] = sortFlag;

            query.SortFlag = sortFlag;
            return query;
        }

        //
        // GET: /Temporary/
        public ActionResult Index()
        {
            Query();
            return View();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stock"></param>
        /// <returns></returns>
        public ActionResult Query()
        {
            Traning_NetworkMeeting_Query query = new Traning_NetworkMeeting_Query();
            GetQuery(query);
            query.keywords = StringTools.Request("keywords");
            string strStartDate = StringTools.Request("StartDate");
            if (!string.IsNullOrEmpty(strStartDate))
            {
                DateTime startDate = DateTime.MinValue;
                if (DateTime.TryParse(strStartDate, out startDate))
                {
                    query.StartDate = startDate;
                }
            }
            else
            {
                query.StartDate = DateTime.Now.Date.AddMonths(-3);
            }
            string strEndDate = StringTools.Request("EndDate");
            if (!string.IsNullOrEmpty(strEndDate))
            {
                DateTime endDate = DateTime.MinValue;
                if (DateTime.TryParse(strEndDate, out endDate))
                {
                    query.EndDate = endDate;
                }
            }
            List<Traning_NetworkMeeting> list = new List<Traning_NetworkMeeting>();
            Traning_NetworkMeetingBLL bll = new Traning_NetworkMeetingBLL();
            list = bll.GetCourse(query);
            ViewData["lst"] = list;
            PageHelper(query);
            return View();
        }

        public ActionResult NetworkMeetingList()
        {
            NetworkMeetingQuery();
            return View();
        }

        public ActionResult NetworkMeetingQuery()
        {
            Traning_NetworkMeeting_Query query = new Traning_NetworkMeeting_Query();
            GetQuery(query);
            query.keywords = StringTools.Request("keywords");
            string strStartDate = StringTools.Request("StartDate");
            if (!string.IsNullOrEmpty(strStartDate))
            {
                DateTime startDate = DateTime.MinValue;
                if (DateTime.TryParse(strStartDate, out startDate))
                {
                    query.StartDate = startDate;
                }
            }
            else
            {
                query.StartDate = DateTime.Now.Date.AddYears(-3);
            }
            string strEndDate = StringTools.Request("EndDate");
            if (!string.IsNullOrEmpty(strEndDate))
            {
                DateTime endDate = DateTime.MinValue;
                if (DateTime.TryParse(strEndDate, out endDate))
                {
                    query.EndDate = endDate;
                }
            }
            List<Traning_NetworkMeeting> list = new List<Traning_NetworkMeeting>();
            Traning_NetworkMeetingBLL bll = new Traning_NetworkMeetingBLL();
            list = bll.GetHistryList(query);
            ViewData["lst"] = list;
            PageHelper(query);
            return View();
        }

        public ActionResult Edit(int id)
        {
            Traning_NetworkMeetingBLL bll = new Traning_NetworkMeetingBLL();
            Traning_NetworkMeeting model = bll.GetModel(id);
            return View(model);
        }

        [HttpPost]
        public void NetworkMeetingExport()
        {
            Traning_NetworkMeeting_Query query = new Traning_NetworkMeeting_Query();
            GetQuery(query);
            query.keywords = StringTools.Request("keywords");
            string strStartDate = StringTools.Request("StartDate");
            if (!string.IsNullOrEmpty(strStartDate))
            {
                DateTime startDate = DateTime.MinValue;
                if (DateTime.TryParse(strStartDate, out startDate))
                {
                    query.StartDate = startDate;
                }
            }
            else
            {
                query.StartDate = DateTime.Now.Date.AddYears(-3);
            }
            string strEndDate = StringTools.Request("EndDate");
            if (!string.IsNullOrEmpty(strEndDate))
            {
                DateTime endDate = DateTime.MinValue;
                if (DateTime.TryParse(strEndDate, out endDate))
                {
                    query.EndDate = endDate;
                }
            }
            List<Traning_NetworkMeeting> list = new List<Traning_NetworkMeeting>();
            Traning_NetworkMeetingBLL bll = new Traning_NetworkMeetingBLL();
            list = bll.GetHistryList(query);

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("EM/EMG账号, UID内部账号, 累计收益, 仓位, 持股个数, 当前风格, 风格激活日 \r\n");
            foreach (var item in list)
            {
                sb.AppendFormat("{0}, {1}, {2}, {3}, {4}, {5}, {6}", item.txtteachar, item.txtreateachar, item.startDate, item.mtg_name, item.meeting_type, item.mtg_id, item.time_flag);
                sb.Append("\r\n");
            }

            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "GB2312";
            Response.AppendHeader("Content-Disposition", "attachment;filename=SyCount.txt");
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
            Response.ContentType = "text/plain";//设置输出文件类型为txt文件。 

            System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
            Response.Write(sb.ToString());
            Response.End();

            //return View();
        }

        [HttpPost]
        public ActionResult Edit(Traning_NetworkMeeting model)
        {
            if (ModelState.IsValid)
            {
                Traning_NetworkMeetingBLL bll = new Traning_NetworkMeetingBLL();
                int count = bll.UpdateNetworkMeeting(model);
                if (count > 0)
                    return RedirectToAction("NetworkMeetingList");
                else
                    ModelState.AddModelError("", "编辑失败");
            }
            return View(model);
        }

        public ActionResult TraningLecturerList()
        {
            Traning_LecturerBLL tbll = new Traning_LecturerBLL();
            List<Traning_Lecturer> list = tbll.GetList();
            return View(list);
        }

        public ActionResult AddMeetingType()
        {
            string id = Request["id"];
            List<Traning_MeetingType> lst = new List<Traning_MeetingType>();
            if (!string.IsNullOrEmpty(id))
            {
                Traning_MeetingTypeBLL bll = new Traning_MeetingTypeBLL();
                lst = bll.GetAllList();
                lst = lst.Where(x => x.id == int.Parse(id)).ToList();
            }
            ViewBag.MTypeLst = lst;
            return View();
        }

        [HttpPost]
        public string AddMeetingType(string meetingType, string id)
        {
            Traning_MeetingType model = new Traning_MeetingType();
            model.meetingType = StringTools.Request("meetingType");
            if (!string.IsNullOrEmpty(StringTools.Request("id")))
            {
                model.id = int.Parse(StringTools.Request("id"));
            }
            if (string.IsNullOrEmpty(model.meetingType))
            {
                return "会议类型不能为空";
            }
            model.isDelete = "0";
            model.createDate = DateTime.Now;
            Traning_MeetingTypeBLL bll = new Traning_MeetingTypeBLL();
            try
            {
                bll.AddUpdate(model);
                return "添加成功";
            }
            catch
            {
                return "添加失败";
            }
        }

        public ActionResult MeetingTypeList()
        {
            Traning_MeetingTypeBLL bll = new Traning_MeetingTypeBLL();
            List<Traning_MeetingType> lst = bll.GetAllList();
            ViewBag.MTypeLst = lst;
            return View();
        }
    }
}
